﻿using Common;
using Common.XmlHandle;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;

namespace SFJDManagement
{
    /// <summary>
    /// Page_Edit_Data.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Edit_Data : Page
    {
        public static List<ClassBrigade> list_Brigade = Runtime.xmlBrigade.GetAll();
        public static List<ClassConfig> list_User = Runtime.xmlUsers.GetAllUsers();
        private string preValue;

        public Page_Edit_Data()
        {
            InitializeComponent();
            Ini();
        }

        private void Ini()
        {
            //生成DataGrid
            DataGrid dataGrid = new DataGrid() { };
            dataGrid.ItemsSource = Runtime.xmlData.GetAllData();
            dataGrid.EnableRowVirtualization = false;
            dataGrid.EnableColumnVirtualization = false;
            dataGrid.HeadersVisibility = DataGridHeadersVisibility.Column;
            dataGrid.AutoGenerateColumns = false;
            dataGrid.CanUserAddRows = false;
            dataGrid.LoadingRow += DataGrid_LoadingRow;
            //自定义事件
            dataGrid.BeginningEdit += DataGrid_BeginningEdit;
            //定义各列
            DataGridTextColumn column_ID = new DataGridTextColumn() {
                Header = "字号",
                Binding = new Binding("ID")
            };
            dataGrid.Columns.Add(column_ID);
            DataGridTextColumn column_Brigade = new DataGridTextColumn() {
                Header = "委托人",
                Binding = new Binding("Brigade") { Converter = new ConverterBrigade() }
            };
            dataGrid.Columns.Add(column_Brigade);
            DataGridTextColumn column_CaseType = new DataGridTextColumn() {
                Header = "案件类型",
                Binding = new Binding("CaseType") { Converter = new ConverterCaseType() }
            };
            dataGrid.Columns.Add(column_CaseType);
            DataGridTextColumn column_User = new DataGridTextColumn() {
                Header = "完稿人",
                Binding = new Binding("User") { Converter = new ConverterUser() }
            };
            dataGrid.Columns.Add(column_User);
            DataGridTextColumn column_Happen = new DataGridTextColumn() {
                Header = "事故时间",
                Binding = new Binding("TimeHappen") { StringFormat = "MM-dd HH:mm"}
            };
            dataGrid.Columns.Add(column_Happen);
            DataGridTextColumn column_Entrust = new DataGridTextColumn()
            {
                Header = "委托时间",
                Binding = new Binding("TimeEntrust") { StringFormat = "MM-dd"}
            };
            dataGrid.Columns.Add(column_Entrust);
            DataGridTextColumn column_Arrange = new DataGridTextColumn()
            {
                Header = "完稿时间",
                Binding = new Binding("TimeArrange") { StringFormat = "MM-dd HH:mm:ss"}
            };
            dataGrid.Columns.Add(column_Arrange);
            IColumnVehicle column_Vehicle = new IColumnVehicle()
            {
                Header = "车辆及项目",
                Binding = new Binding("Vehicles")
            };
            dataGrid.Columns.Add(column_Vehicle);

            //添加入布局
            Content = dataGrid;
        }

        private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            preValue = (e.Column.GetCellContent(e.Row) as TextBlock).Text;
            System.Diagnostics.Debug.Print(preValue);
        }

        //设置隔行变色
        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            //ItemsControl items = sender as ItemsControl;
            //switch(e.Row.GetIndex() % 2)
            //{
            //    case 1:
            //        e.Row.Background = Brushes.Lavender;
            //        break;
            //}
        }

        public class ConverterBrigade : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string b = "ERRO";
                foreach(ClassBrigade brigade in list_Brigade)
                {
                    if(brigade.Guid.ToString() == value.ToString())
                    {
                        b = brigade.FullName;
                        break;
                    }
                }
                return b;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string b = Guid.Empty.ToString();
                foreach (ClassBrigade brigade in list_Brigade)
                {
                    if (brigade.FullName == value.ToString())
                    {
                        b = brigade.Guid.ToString();
                        break;
                    }
                }
                return b;
            }
        }

        public class ConverterUser : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string b = "ERRO";
                foreach (ClassConfig user in list_User)
                {
                    if (user.UserGuid.ToString() == value.ToString())
                    {
                        b = user.UserName;
                        break;
                    }
                }
                return b;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string b = Guid.Empty.ToString();
                foreach (ClassConfig user in list_User)
                {
                    if (user.UserName == value.ToString())
                    {
                        b = user.UserGuid.ToString();
                        break;
                    }
                }
                return b;
            }
        }

        public class ConverterCaseType : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ((EnumCaseType)(int.Parse(value.ToString()))).ToString();
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return Enum.Parse(typeof(EnumCaseType), value.ToString());
            }
        }
    }
}
